<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/json2.js"></script>
		<style type="text/css">
			#provider {
				margin-left: 16%;
			}
		</style>
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div style="float:right;margin-top:0px;max-width:60px;" id="notreview"></div>
		<div>

			<form id="ff" method="post">

				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="20%">供应商</td>
						<td width="30%">
							<input class="easyui-combobox" id="input_PROVIDER_NAME" style="width: 100%" name="PROVIDER_NAME" data-options="required:true">
						</td>
						<td width="20%">采购单单号</td>
						<td width="30%">
							<input class="easyui-textbox" id="input_PURCHASEORDER_NO" name="PURCHASEORDER_NO" style="width: 100%" data-options="disabled:true">
						</td>

					</tr>
					<tr>
						<td>入库单单号</td>
						<td>
							<input class="easyui-textbox" style="width: 100%" id="input_WAREHOUSEORDER_NO" name="WAREHOUSEORDER_NO" data-options="prompt:'由系统自动生成，不需填写',disabled:true" />
						</td>
					</tr>
					<tr id="nodisplay" style="display: none">
						<td>供应商ID</td>
						<td>
							<input class="easyui-textbox" style="width: 100%" id="input_PROVIDER_ID" name="PROVIDER_ID" data-options="disabled:true" />
						</td>
						<td>采购单ID</td>
						<td>
							<input class="easyui-textbox" style="width: 100%" id="input_PURCHASEORDER_ID" name="PURSECHASEORDER_ID" data-options="disabled:true" />
						</td>

					</tr>
					<tr id="nodisplay" style="display: none">
						<td>入库单ID</td>
						<td>
							<input class="easyui-textbox" style="width: 100%" id="input_WAREHOUSEORDER_ID" name="WAREHOUSEORDER_ID" data-options="disabled:true" />
						</td>
						<td>库房ID</td>
						<td>
							<input class="easyui-textbox" style="width: 100%" id="input_STOREHOUSE_ID" name="STOREHOUSE_ID" data-options="disabled:true" />
						</td>

					</tr>
				</table>
				<div id="p" class="easyui-panel" title="供应商品详细信息" style="width:100%;">
					<table id="tt">

					</table>
				</div>
				<div style="padding: 5px 0px ;">
					<table>
						<tr>
							<th>供货总价合计</th>
							<td>
								<input type="text" style="border: none;" name="number" />
							</td>
							<th>供货总价合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none;" id="dx" />
							</td>
						</tr>
					</table>
					<div style="margin-left: 20%;" id="new_footer">

					</div>
				</div>
			</form>
		</div>
	</body>
	<script type="text/javascript">
		var ids = getMultiID();
		var updateData; //获取到的更新数据
		var _dataPage;
		var updateLog = "";
		var currid;
		var isVerify;
		///ids.WarehouseID表示传入的入库单编号
		var _url = "";
		$(document).ready(function() {
			if(ids.WarehouseID != AddFlag) { //修改的情况
				_url = RemoteLinkAddress + '/logistics/warehousein/findbyid.do?ID=' + ids.WarehouseID;
				currid = ids.WarehouseID;
				loadRemote(_url);
			} else //新增的情况
			{
				_url = RemoteLinkAddress + '/logistics/purchaseorder/findbyid.do?ID=' + ids.PurchaseOrderid;
				currid = ids.PurchaseOrderid;
				loadRemote(_url);
			}
		});
		addFooter($("#new_footer"));
		var receipts_type = RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_VERIFY_SYMBOL;
		receipts_type += RECEIPT_UNVERIFY_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL + RECEIPT_DELETE_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/
		function searchmore() {
			AddTabIn("入库单查询", parent.$('#tabs'), "UI/LOGISTICS/WareHouseScheduleProvider/WareHouseScheduleProviderSearch.html");
		}

		function verifydata() {
			if(ids.WarehouseID == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行审核操作！");
				return;
			}
			var a = checkDataIntegrity();
			if(isVerify == 1) {
				$.messager.alert("提示", "该订单已通过审核！无需再审核！");
				return;
			}
			if(a == 0) {
				$.messager.alert("失败", "请将页面中的数据补充完整后再进行审核！");
				return;
			}
			$.messager.confirm('Confirm', '是否审核', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/logistics/warehousein/verify.do", {
							"ID": currid,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({ // show error message
									title: '成功',
									msg: '审核成功!'
								});
								isVerify = 1;
								verifyStatus(isVerify, true);
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function unverifydata() {
			if(isVerify == 0) {
				$.messager.alert("提示", "该订单已弃审！无需再弃审！");
				return;
			}
			if(ids.WarehouseID == AddFlag) {
				$.messager.alert("提示", "该页面为新增页面，不能进行弃审操作！");
				return;
			}
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/logistics/warehousein/unverify.do", {
							"ID": currid,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({ // show error message
									title: '成功',
									msg: '放弃审核成功!'
								});
								isVerify = 0;
								verifyStatus(isVerify, true);
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function deletedata() {
			if(ids.WarehouseID != AddFlag) {
				if(isVerify == 1) {
					$.messager.alert("警告", "此页面已审核，不能删除");
				} else {
					$.messager.confirm('Confirm', '是否删除', function(r) {
						if(r) {
							var sendData = getAllData("delete");
							$.post(RemoteLinkAddress + "/logistics/warehousein/delete.do", {
									"ID": currid
									//									a: sendData
								},
								function(result) {
									if(result.msg == "success") {
										$.messager.alert({ // show success message
											title: '成功',
											msg: '删除成功'
										});
									} else {
										$.messager.alert({ // show error message
											title: 'Error',
											msg: result.msg
										});
									}
								},
								'json');
						}
					});
				}
			} else {
				$.messager.alert("警告", "此页面是新增页面，不能执行");
			}
		}

		function print() {
			if(isVerify == 0) {
				$.messager.alert("警告", "入库单未审核不能打印");
				return;
			}
			if(ids.WarehouseID != AddFlag) {
				var pLocation = "";
				pLocation = "WareHouseScheduleProviderPrint.html?id=" + currid;
				window.open(pLocation);
			} else {
				$.messager.alert("警告", "此页面是新增页面不能打印");
			}
		}

		function loadRemote(_url) {
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(ids.WarehouseID != AddFlag) {
						isVerify = result.ISVERIFY;
						verifyStatus(isVerify, true);
					}
					var _data = constructWarehouseInData(result, ids.WarehouseID);
					_dataPage = result;
					//					_dataPage.ISVERIFY = 0;
					updateData = deepCopy(result);
					getFormData(_data);
					for(var data of _data.rows) {
						data.QUALITY = "高级";
					}
					$("#number").html(result.REAL_SUM);
					$('#tt').edatagrid('loadData', _data.rows); //调用easyui的loadData方法，将数据写入id为tt的datagrid中
				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		var _detailToolbarData = AddDetailToolBar(2);
		GetTable();

		function GetTable() {
			$('#tt').edatagrid({
				height: 290,
				width: '100%',
				toolbar: _detailToolbarData,
				collapsible: true,
				singleSelect: true,
				fitColumns: true,
				autoSave: true,
				columns: [
					[{
							field: 'ID',
							title: '明细ID',
							width: '5%',
							align: 'center',
						}, {
							field: 'INTERNAL_TYPE',
							title: '商品类型',
							width: '5%',
							align: 'center',
						},
						{
							field: 'INTERNALTRADE_ID',
							title: '商品ID',
							width: '9%',
							align: 'center',
						}, {
							field: 'UNIT_NAME',
							title:'原始单位',
							width:'5%',
							align:'center'
						}, {
							field: 'INTERNAL_NAME',
							title: '品名',
							width: '10%',
							align: 'center',
							editable: false,
							required: true,
						}, {
							field: 'EVALUATE_AMOUNT',
							title: '应入库数量',
							width: '5%',
							align: 'center',
							required: true,
							precision: 2,
							groupSeparator: ',',
							decimalSeparator: '.',
						}, {
							field: 'REAL_AMOUNT',
							title: '已入库数量',
							width: '5%',
							align: 'center',
							required: true,
							precision: 2,
							groupSeparator: ',',
							decimalSeparator: '.',
						},
						{
							field: 'QUALITY',
							title: '产品质量',
							width: '10%',
							align: 'center',
							editor: {
								type: 'combobox',
								options: {
									url: RemoteLinkAddress + '/logistics/level/listall.do',
									textField: 'LEVEL',
									valueField: 'LEVEL'
								}
							},
						}, {
							field: 'PROVIDER_AMOUNT',
							title: '供货数量',
							width: '10%',
							align: 'center',
							editor: {
								type: 'numberbox',
								options: {
									required: true,
									precision: 2,
									groupSeparator: ',',
									decimalSeparator: '.',
								}
							},
						},{
							field: 'PROVIDER_PRICE',
							title: '供货价格',
							width: '10%',
							align: 'center',
							editor: {
								type: 'numberbox',
								options: {
									precision: 2,
									editable: true,
									type: "password",
								}
							},
							formatter: function(value, row, index) {
								if(value && row.INTERNAL_TYPE != "蔬菜类" && row.INTERNAL_TYPE != "水果类") {
									return ".";
								} else {
									return value;
								}
							}
						},  {
							field: 'INOUT_UNIT',
							title:'单位',
							width:'5%',
							align:'center',
							editor: {
								type: 'textbox',
								options: {
									type: "text",
								}
							},
							formatter: function(value, row, index) {
								if(row.INOUT_UNIT === '' || row.INOUT_UNIT === undefined){
									row.INOUT_UNIT = row.UNIT_NAME
								}
								return row.INOUT_UNIT;
							}
						}, {
							field: 'PROVIDER_SUM',
							title: '小计',
							width: '10%',
							align: 'center',
							editor: {
								options: {
									required: true,
									precision: 2,
									groupSeparator: ',',
									decimalSeparator: '.',
									prefix: '￥'
								}
							},
							formatter: function(value, row, index) {
								if(value) {
									return "￥" + value;
								}
							}
						},
					]
				],

				onEndEdit: function(index, row, changes) {
					if(row.INTERNAL_TYPE == "蔬菜类" || row.INTERNAL_TYPE == "水果类") {
						var realamount = row.PROVIDER_AMOUNT;
						var realprice = row.PROVIDER_PRICE;
						var result = realamount * realprice;
					} else {
						result = 0;
					}
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							PROVIDER_SUM: result.toFixed(2)
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					var _num = 0;
					var GridRows = $("#tt").datagrid("getRows");
					for(var i = 0; i < GridRows.length; i++) {
						if(GridRows[i].PROVIDER_AMOUNT > 0) {
							_num += parseFloat(GridRows[i].PROVIDER_SUM);
						}
					}
					var num = changeMoneyToChinese(_num.toFixed(2));
					$("#dx").val(num);
					$("input[name='number']").val(_num);
					if(ids.WarehouseID != AddFlag) {
						if(row.isNewRecord == false || row.isNewRecord == undefined) //即修改的情况
						{
							for(var i = 0; i < updateData.rows.length; i++) {
								if(updateData.rows[i].ID == row.ID) {
									var obj = updateData.rows[i];
									for(var s in obj) {
										if(obj[s] != row[s]) {
											var tmp_updatelog = "[修改记录]:[品种]" + row.INTERNAL_NAME + "[" + searchOrderFieldMap(s) + "] 原值为:" + obj[s] + " 新值为:" + row[s] + ";";
											updateLog += tmp_updatelog;
											obj[s] = row[s];
										}
									}
								}
							}
						}
					}

				}

			});
		}

		function save() {
			if(isVerify == 1) {
				$.messager.alert("提示", "审核通过后不允许修改");
				return;
			}
			if($("input[name='PROVIDER_NAME']").val() == "") {
				$.messager.alert("提示", "订单信息不能为空！");
				return;
			}
			if(ids.WarehouseID != AddFlag) {
				getUpdateData();
			} else {
				getSaveData();
			}

		}
		$('#input_PROVIDER_NAME').combobox({ //选中id为customer_name的combobox
			valueField: 'PROVIDER_NAME',
			textField: 'PROVIDER_NAME',
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/provider/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) { //当选中数据后调用该function，并得到该combobox对应的valuefield和textfield，赋值给record
				$('#input_PROVIDER_ID').textbox('setValue', record.ID);
			} //调用easyui的te/调用easyui的textbox的写入值方法，将record中name为CUSTOMER_ID的写入id为customer_id的textbox中
		});

		function getUpdateData() {
			updateData.ID = $("input[name='WAREHOUSEORDER_ID']").val();
			updateData.ORDER_NO = $("input[name='WAREHOUSEORDER_NO']").val();
			updateData.PURCHASEORDER_ID = $("input[name='PURSECHASEORDER_ID']").val();
			updateData.MEMO = $("input[name='MEMO']").val();
			updateData.REAL_SUM = $("input[name='number']").val();
			updateData.PROVIDER_ID = $("input[name='PROVIDER_ID']").val();
			updateData.LOG = updateLog;
			var sendData = JSON.stringify(updateData);
			submitData(sendData);
		}

		function getSaveData() {
			var sendData = getAllData("save");
			submitData(sendData);
		}

		function submitData(_data) {
			var _url = "";
			if(ids.WarehouseID == AddFlag) {
				_url = RemoteLinkAddress + "/logistics/warehousein/save.do";
			} else {
				_url = RemoteLinkAddress + "/logistics/warehousein/update.do"
			}
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _data,
				type: "POST",
				success: function(result) {
					if(ids.WarehouseID == AddFlag) {
						$.messager.alert('提示信息', '新增成功!');
						var msg = result.msg;
						ids.WarehouseID = msg.split(";")[1];
						currid = msg.split(";")[1];
					} else {
						$.messager.alert('提示信息', '更新成功!');
					}

				},
				error: function(result) {
					if(ids.WarehouseID == AddFlag) {
						$.messager.alert('提示信息', '新增失败');
					} else {
						$.messager.alert('提示信息', '更新失败');
					}

				}
			});
		}

		function constructWarehouseInData(oldData, type) //构建入库信息
		{
			var newData = Object();
			for(var s in oldData) {
				newData[s] = oldData[s];
			}
			if(type == AddFlag) {
				newData.WAREHOUSEORDER_ID = "";
				newData.WAREHOUSEORDER_NO = "";
				newData.PURCHASEORDER_NO = oldData.ORDER_NO;
				newData.PURCHASEORDER_ID = oldData.ID;
			} else {
				newData.WAREHOUSEORDER_ID = oldData.ID;
				newData.WAREHOUSEORDER_NO = oldData.ORDER_NO;
			}
			return newData;
		}

		function getAllData(type) {
			var data = new Object();
			data = submitFormData();
			data.REAL_SUM = $("input[name='number']").val();
			if(type == "delete") {
				data.ID = $("input[name='WAREHOUSEORDER_ID']").val();
			}
			data.rows = new Array();
			var dataGridRows = $("#tt").datagrid("getRows");
			var _index = 0;
			for(var i = 0; i < dataGridRows.length; i++) {
				if(parseFloat(dataGridRows[i].PROVIDER_AMOUNT) > 0) {
					data.rows[_index] = new Object();
					data.rows[_index].PURCHASEORDERDETAIL_ID = dataGridRows[i].ID;
					data.rows[_index].INTERNALTRADE_ID = dataGridRows[i].INTERNALTRADE_ID;
					data.rows[_index].QUALITY = dataGridRows[i].QUALITY;
					data.rows[_index].PROVIDER_PRICE = dataGridRows[i].PROVIDER_PRICE;
					data.rows[_index].PROVIDER_AMOUNT = dataGridRows[i].PROVIDER_AMOUNT;
					data.rows[_index].PROVIDER_SUM = dataGridRows[i].PROVIDER_SUM;
					data.rows[_index].PROVIDER_ID = dataGridRows[i].PROVIDER_ID;
					data.rows[_index].INOUT_UNIT = dataGridRows[i].INOUT_UNIT;
					_index = _index + 1;
				}
			}
			var _data = JSON.stringify(data);
			return _data;
		}

		function checkDataIntegrity() {
			var providername = $("input[name='PROVIDER_NAME']").val();
			var rowsdata = $("#tt").datagrid('getRows');
			if(providername != "") {
				if(rowsdata.length != 0) {
					for(var i = 0; i < rowsdata.length; i++) {
						var internaltype = rowsdata[i].INTERNAL_TYPE;
						var internalname = rowsdata[i].INTERNAL_NAME;
						var unitname = rowsdata[i].UNIT_NAME;
						var unitprice = rowsdata[i].UNIT_PRICE;
						var amount = rowsdata[i].AMOUNT;
						if(internalname == "" || internaltype == "" || unitname == "") {
							return 0;
						}
					}
				} else {
					return 0;
				}
			} else {
				return 0;
			}
		}
	</script>

</html>